package com.jysz.certificate.persistence.dao.system;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.jysz.certificate.persistence.dao.AbstractBaseDao;
import com.jysz.certificate.persistence.model.system.Oper;
import com.jysz.certificate.persistence.model.system.RoleOperRef;
import com.jysz.certificate.persistence.model.system.User;

/**
 * 权限管理-权限
 * @author xuxiaolong
 *
 */
@Repository
public class OperDao extends AbstractBaseDao<Oper, Long> {
	
	public Oper findByCode(String code){
		Criteria c=this.createCriteria();
		c.add(Restrictions.eq("code", code));
		return (Oper) c.uniqueResult();
	}

	public boolean checkPermission(User user,String code){
		
		Criteria criteria=this.getSession().createCriteria(RoleOperRef.class);
		criteria.createCriteria("oper").add(Restrictions.eq("code", code));
		criteria.add(Restrictions.in("role", user.getRoles()));
		
		List l=criteria.list();
		if(l!=null&&l.size()>0)
			return true;
		return false;
		
	}
}
